<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/20
 * Time: 17:21
 */
    namespace Addons\IcWaterGoods\Model;

    class IcGoodsTypesModel extends PublicModel{
        /*获取商品分类，按照一定的格式输出*/
        public function getTypesLists(){
            $types = $this->getListAll('',null,false,'pid ASC');
            $arr   = array();
            $pidarr = array();
            foreach($types as $key => $val){
                if($val['pid']){
                    $arr[$pidarr[$val['pid']]][] = $val;
                }else{
                    $pidarr[$val['id']] = $val['type_name'];
                }
            }

            return $arr;
        }

        public function getUserTypesLists(){
            $types = $this->getListAll('',null,false,'pid ASC');
            $openid = get_openid();
            $usertypes = json_decode(M('IcFollow')->where(array('openid'=>$openid))->getField('fid'));
            $arr   = array();
            $pidarr = array();
            foreach($types as $key => $val){
                if($val['pid']){
                    if(in_array($val['id'],$usertypes)) {
                        $arr[$pidarr[$val['pid']]][] = $val;
                    }
                }else{
                        $pidarr[$val['id']] = $val['type_name'];
                }
            }

            return $arr;
        }

        public function getFirstType($typeList) {
            $d = "";
            foreach ($typeList as $k=>$type) {
                $d = $k;
                $d .= " " . $type[0]['type_name'];
            }
            return $d;
        }

        public function getTypeIdByStr($str) {
            $type = explode(" ", $str);
            $typeId = $this->getOne(array('type_name'=>$type[count($type) - 1]), "id");
            return $typeId['id'];
        }

        public function getTypeStrById($id) {
            $type = $this->getOne(array('id'=>$id));
            $parent = $this->getOne(array('id'=>$type['pid']));
            return $parent['type_name'] . " " . $type['type_name'];
        }

        public function getTypesList(){
            $data = $this->getListAll('',array('pid'=>array('neq',0)));
            $arr = array();
            foreach($data as $val){
                $arr[$val['id']] = $val['type_name'];
            }
            return $arr;
        }
    }